import requests
from bs4 import BeautifulSoup
import bs4

def getHtmlText(url):
    try:
        res = requests.get(url)
        res.raise_for_status()
        res.encoding = res.apparent_encoding
        return res.text
    except:
        return ""

def fillUniList(uniList, html):
    soup = BeautifulSoup(html, 'html.parser')
    for tr in soup.find('tbody').children:
        if isinstance(tr, bs4.element.Tag):
            tds=tr('td')
            tds[1]=tds[1]('a')[0]
            uniList.append([tds[i].string.strip() for i in range(3)])

def printUniList(uniList, num=10):
    tmpl="{0:^10}\t{1:{3}^10}\t{2:^10}"
    print(tmpl.format("排名", "学校名称", "省份", chr(12288)))
    for i in range(num):
        u = uniList[i]
        print(tmpl.format(u[0], u[1], u[2], chr(12288)))

if __name__ == '__main__':
    html=getHtmlText('https://www.shanghairanking.cn/rankings/bcur/2020')
    uniList=[]
    fillUniList(uniList, html)
    printUniList(uniList, 100)